package com.sywb.chuangyebao.contract;

import android.app.Activity;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.AddrInfoBean;
import com.sywb.chuangyebao.bean.CategoryBean;
import com.sywb.chuangyebao.bean.FindProjectListBean;
import com.sywb.chuangyebao.utils.GlideUtils;
import com.sywb.chuangyebao.utils.HttpCallback;
import com.sywb.chuangyebao.utils.HttpUtils;
import com.sywb.chuangyebao.utils.Utils;
import com.sywb.chuangyebao.view.BusinessDetailsActivity;

import org.bining.footstone.adapter.BaseRecyclerAdapter;
import org.bining.footstone.adapter.ViewHolderHelper;
import org.bining.footstone.adapter.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by pxb on 2018/6/11.
 */

public interface SearchJoinContract {

    interface SearchJoinView extends BaseRecyclerContract.View {

        TextView clickSearch();

        EditText getSearch();

        RecyclerView addressRecycler();

        RecyclerView industryRecycler();

        RecyclerView secondIndustryRecycler();

        String getClickID();

        String getClickSecID();

        List<CategoryBean> getDataCategory();

        TextView tvIndustry();

        TextView tvAddress();

        void changePosition(int position);

    }

    class SearchJoinPresenter extends BaseRecyclerContract.Presenter<SearchJoinView> {
        private BrandRecommendationAdapter recommendationAdapter = null;
        private AddressAdapter addressAdapter = null;
        private RecyclerView addressRecycler;
        private IndustryAdapter industryAdapter = null;
        private SecondIndustryAdapter secondIndustryAdapter = null;
        private RecyclerView industryRecycler;
        private RecyclerView secondIndustryRecycler;
        private List<CategoryBean> stringArray = new ArrayList<>();
        private int pageNo = 1;
        private String editSearch = "";
        private int addrId;//地址ID
        private int cardID;//二级行业ID
        private List<FindProjectListBean> mFindProjectListBean;

        @Override
        public void onStart() {
            super.onStart();
            mView.clickSearch().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    editSearch = mView.getSearch().getText().toString().trim();
                    if (editSearch.isEmpty()) {
                        showMessage(R.string.input_something);
                    } else {
                        initProjectList();
                    }
                }
            });
            mView.getSearch().setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                        editSearch = mView.getSearch().getText().toString().trim();
                        if (editSearch.isEmpty()) {
                            showMessage(R.string.input_something);
                        } else {
                            initProjectList();
                        }
                        return true;
                    }
                    return false;
                }
            });

            initProjectAddr();

            //找项目搜索列表页
            recommendationAdapter = new BrandRecommendationAdapter(mContext);
            setRecyclerAdapter(recommendationAdapter);

            initData();

            //region 地址数据
            addressRecycler = mView.addressRecycler();
            addressAdapter = new AddressAdapter(mContext);
            addressRecycler.setLayoutManager(new LinearLayoutManager(mContext));

            addressAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(View view, int i) {
                    addressAdapter.setAttribute(i);
                    addrId = addressAdapter.getItem(i).getId();
                    mView.tvAddress().setText(addressAdapter.getItem(i).getName());
                    mView.changePosition(-1);
                    pageNo = 1;
                    initProjectList();
                }
            });
            addressRecycler.setAdapter(addressAdapter);

            //endregion

            //加盟行业一级
            industryRecycler = mView.industryRecycler();
            industryAdapter = new IndustryAdapter(mContext);
            industryRecycler.setLayoutManager(new LinearLayoutManager(mContext));
            industryRecycler.setAdapter(industryAdapter);

            //加盟行业二级
            secondIndustryRecycler = mView.secondIndustryRecycler();
            secondIndustryAdapter = new SecondIndustryAdapter(mContext);
            secondIndustryRecycler.setLayoutManager(new LinearLayoutManager(mContext));
            secondIndustryRecycler.setAdapter(secondIndustryAdapter);

            final List<CategoryBean> firstCategoryBean = mView.getDataCategory();//一级行业中转数据
            final List<CategoryBean.ListBean> secondCategoryBean = new ArrayList<>();//二级行业中转数据

            stringArray.add(0, new CategoryBean(-1, "全部行业"));
            stringArray.addAll(firstCategoryBean);

                for (int i = 0; i < stringArray.size(); i++) {
                    if (Integer.parseInt(mView.getClickID()) == stringArray.get(i).getId()) {
                        if (mView.getClickSecID() != null && !mView.getClickSecID().equals("")) {
                            for (int j = 0; j < stringArray.get(i).getList().size(); j++) {
                                if (Integer.parseInt(mView.getClickSecID()) == stringArray.get(i).getList().get(j).getId()) {
                                    secondIndustryAdapter.selectedPosition = j+1;
                                    secondIndustryRecycler.scrollToPosition(j+1);
                                    secondIndustryAdapter.setAttribute(j+1);
                                    mView.tvIndustry().setText(stringArray.get(i).getList().get(j).getName());
                                    cardID = stringArray.get(i).getList().get(j).getId();
                                    pageNo = 1;
                                    initProjectList();
                                }
                            }
                        }
                        industryAdapter.setAttribute(i);
                        industryRecycler.scrollToPosition(i);

                        secondCategoryBean.add(0, new CategoryBean.ListBean(stringArray.get(i).getId(), "全部"));
                        secondCategoryBean.addAll(stringArray.get(i).getList());
                        secondIndustryAdapter.setDatas(secondCategoryBean);
                    }
                }

                industryAdapter.setDatas(stringArray);

                industryAdapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int i) {
                        industryAdapter.setAttribute(i);
                        for (int j = 0; j < secondIndustryAdapter.getItemCount(); j++) {
                            if (secondIndustryAdapter.selectedPosition == j) {
                                secondIndustryAdapter.selectedPosition = -1;
                            }
                        }
                        List<CategoryBean.ListBean> secondBean = new ArrayList<>();
                        if(stringArray.get(i).getList() == null){
                            secondBean.add(0,new CategoryBean.ListBean(-1,"全部"));
                        }else {
                            secondBean = stringArray.get(i).getList();
                            secondBean.set(0, new CategoryBean.ListBean(stringArray.get(i).getId(), "全部"));
                        }
                        secondIndustryAdapter.setDatas(secondBean);
                        secondIndustryAdapter.notifyDataSetChanged();
                    }
                });



            secondIndustryAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(View view, int i) {
                    secondIndustryAdapter.setAttribute(i);

                    if (secondIndustryAdapter.getItem(i).getName().equals("全部")) {
                        switch (secondIndustryAdapter.getItem(i).getId()) {
                            case 2:
                                mView.tvIndustry().setText("餐饮美食");
                                mView.changePosition(-1);
                                break;
                            case 1:
                                mView.tvIndustry().setText("服装鞋包");
                                mView.changePosition(-1);
                                break;
                            case 6:
                                mView.tvIndustry().setText("家居用品");
                                mView.changePosition(-1);
                                break;
                            case 5:
                                mView.tvIndustry().setText("礼品饰品");
                                mView.changePosition(-1);
                                break;
                            case 8:
                                mView.tvIndustry().setText("建材装饰");
                                mView.changePosition(-1);
                                break;
                            case 9:
                                mView.tvIndustry().setText("环保机械");
                                mView.changePosition(-1);
                                break;
                            case 4:
                                mView.tvIndustry().setText("美容保养");
                                mView.changePosition(-1);
                                break;
                            case 7:
                                mView.tvIndustry().setText("母婴用品");
                                mView.changePosition(-1);
                                break;
                            case 3:
                                mView.tvIndustry().setText("生活服务");
                                mView.changePosition(-1);
                                break;
                            case 12:
                                mView.tvIndustry().setText("酒水饮料");
                                mView.changePosition(-1);
                                break;
                            case 10:
                                mView.tvIndustry().setText("汽车服务");
                                mView.changePosition(-1);
                                break;
                            case 11:
                                mView.tvIndustry().setText("教育网络");
                                mView.changePosition(-1);
                                break;
                            case -1:
                                mView.tvIndustry().setText("全部行业");
                                mView.changePosition(-1);
                                break;
                        }
                    } else {
                        mView.tvIndustry().setText(secondIndustryAdapter.getItem(i).getName());
                        mView.changePosition(-1);
                    }
                    cardID = secondIndustryAdapter.getItem(i).getId();
                    pageNo = 1;
                    initProjectList();
                }
            });
        }

        @Override
        public void initData() {
            super.initData();
            onStartAsync();
        }

        @Override
        public void onStartLoadMore() {
            super.onStartLoadMore();
            onStartAsync();
            pageNo++;
            initProjectList();
        }

        /**
         * 项目列表接口数据
         */
        private void initProjectList() {
            onStartAsync();
            HttpUtils.getSearchResultList(editSearch, cardID, addrId, 10, pageNo, new
                    HttpCallback<List<FindProjectListBean>>() {
                        @Override
                        public void onSuccess(List<FindProjectListBean> beans) {
                            mFindProjectListBean = beans;
                            if (beans.size() == 0) {
                                mView.setNoData(true);
                            } else {
                                mView.setNoData(false);
                            }
                            if (pageNo == 1) {
                                onEndRefresh();
                                recommendationAdapter.setFooterView(null);
                                recommendationAdapter.clearDatas();
                            } else {
                                if (beans != null && beans.size() >= 0) {
                                    recommendationAdapter.setFooterView(R.layout.layout_footer);
                                } else {
                                    recommendationAdapter.setFooterView(null);
                                }
                            }
                            recommendationAdapter.notifyDataChangedAfterLoadMore(beans);
                        }

                        @Override
                        public void onError(String strMsg) {
                            super.onError(strMsg);
                            if (pageNo == 1) {
                                onEndRefresh();
                            } else {
                                onEndLoadMore();
                            }
                            showMessage(strMsg);
                        }

                        @Override
                        public void onFinish() {
                            super.onFinish();
                            onFinishAsync();
                            isNoData();
                        }
                    });
        }

        /**
         * 加盟地址
         */
        private void initProjectAddr() {
            HttpUtils.getAddrInfo(new HttpCallback<List<AddrInfoBean>>() {
                @Override
                public void onSuccess(List<AddrInfoBean> addrInfoBeans) {
                    List<AddrInfoBean> temporaryList = new ArrayList<>();
                    temporaryList.add(0, new AddrInfoBean(0, "全部"));
                    temporaryList.addAll(addrInfoBeans);
                    addressAdapter.setDatas(temporaryList);
                }
            });
        }


        @Override
        public void onRecyclerItemClick(int postion) {
            super.onRecyclerItemClick(postion);
            mView.advance(BusinessDetailsActivity.class, mFindProjectListBean.get(postion).getProjectid());
        }

        /**
         * 列表页
         */
        class BrandRecommendationAdapter extends BaseRecyclerAdapter<FindProjectListBean> {

            public BrandRecommendationAdapter(Context context) {
                super(context, R.layout.item_brand_recommend);
            }

            @Override
            protected void setItemData(ViewHolderHelper viewHolderHelper, int i, final FindProjectListBean
                    info) {
                ImageView imageView = (ImageView) viewHolderHelper.getView(R.id.iv_img);
                TextView tvConsult = (TextView) viewHolderHelper.getView(R.id.tv_consult);
                tvConsult.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (info.getProjectid() != null) {
                            Utils.goConsult(SearchJoinPresenter.this, mView, Integer.parseInt(info.getProjectid())
                                    , 0, Constants.COOKIE_PROJECT);
                        }
                    }
                });
                if (info.getLogo() != null) {
                    GlideUtils.getNormalIamgeToUrl((Activity) mContext, info.getLogo(), imageView);
                }
                if (info.getTitle() != null && !info.getTitle().equals("")) {
                    viewHolderHelper.setText(R.id.tv_title, info.getProject_title());
                } else {
                    viewHolderHelper.setText(R.id.tv_title, R.string.no_data);
                }
                viewHolderHelper.setText(R.id.tv_industry_style, info.getCat_name());
                /*if (info.getOnline().equals("1")){
                    viewHolderHelper.setVisibility(R.id.recommend_tv,true);
                }else {
                    viewHolderHelper.setVisibility(R.id.recommend_tv,false);
                }*/
                viewHolderHelper.setText(R.id.tv_amount_num, info.getOnline().equals("1")?info.getInvestment():"面议");
                viewHolderHelper.setText(R.id.tv_views_num, info.getCount().getHits() + "");
            }

        }

        /**
         * 总部地址
         */
        class AddressAdapter extends BaseRecyclerAdapter<AddrInfoBean> {
            public int selectedPosition = -1;

            public AddressAdapter(Context context) {
                super(context, R.layout.item_address);
            }

            @Override
            protected void setItemData(ViewHolderHelper viewHolderHelper, int i, AddrInfoBean str) {
                viewHolderHelper.setText(R.id.item_address_tv, str.getName());
                viewHolderHelper.setTag(R.id.item_address_tv, str.getId());
                TextView textView = (TextView) viewHolderHelper.getView(R.id.item_address_tv);
                if (selectedPosition != i) {
                    viewHolderHelper.setTextColor(R.id.item_address_tv, ContextCompat.getColor(mContext, R.color.myTextBlack));
                    textView.setTextSize(14);
                } else {
                    viewHolderHelper.setTextColor(R.id.item_address_tv, ContextCompat.getColor(mContext, R.color.popupTextColor));
                    textView.setTextSize(15);
                }
            }

            private void setAttribute(int postition) {
                selectedPosition = postition;
                notifyDataSetChanged();
            }
        }

        /**
         * 加盟行业
         */
        class IndustryAdapter extends BaseRecyclerAdapter<CategoryBean> {
            public int selectedPosition = -1;

            public IndustryAdapter(Context context) {
                super(context, R.layout.item_all_category);
            }

            @Override
            protected void setItemData(ViewHolderHelper viewHolderHelper, int i, CategoryBean str) {
                viewHolderHelper.setText(R.id.item_category_tv, str.getName());
                TextView textView = (TextView) viewHolderHelper.getView(R.id.item_category_tv);
                if (selectedPosition == i) {
                    viewHolderHelper.setBackgroundColor(R.id.item_category_ll, ContextCompat.getColor(mContext, R.color.white));
                    viewHolderHelper.setVisibility(R.id.item_category_indicator_view, true);
                    textView.setTextColor(mContext.getResources().getColor(R.color.colorTheme));
                    textView.setTextSize(15);
                } else {
                    viewHolderHelper.setBackgroundColor(R.id.item_category_ll, ContextCompat.getColor(mContext, R.color.colorManuscript));
                    viewHolderHelper.setVisibility(R.id.item_category_indicator_view, false);
                    textView.setTextColor(mContext.getResources().getColor(R.color.myTextBlack));
                    textView.setTextSize(14);
                }
            }

            private void setAttribute(int postition) {
                selectedPosition = postition;
                notifyDataSetChanged();
            }
        }

        /**
         * 加盟行业二级
         */
        class SecondIndustryAdapter extends BaseRecyclerAdapter<CategoryBean.ListBean> {

            int selectedPosition = -1;

            public SecondIndustryAdapter(Context context) {
                super(context, R.layout.item_address);
            }

            @Override
            protected void setItemData(ViewHolderHelper viewHolderHelper, int i, CategoryBean.ListBean s) {
                viewHolderHelper.setText(R.id.item_address_tv, s.getName());
                TextView textView = (TextView) viewHolderHelper.getView(R.id.item_address_tv);
                if (selectedPosition != i) {
                    viewHolderHelper.setTextColor(R.id.item_address_tv, ContextCompat.getColor(mContext, R.color.myTextBlack));
                    textView.setTextSize(14);
                } else {
                    viewHolderHelper.setTextColor(R.id.item_address_tv, ContextCompat.getColor(mContext, R.color.popupTextColor));
                    textView.setTextSize(15);
                }
            }

            private void setAttribute(int postition) {
                selectedPosition = postition;
                notifyDataSetChanged();
            }
        }

    }

}
